package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    // 查询列表
    @GetMapping
    public Result list(@RequestParam("name") String name, String label,
                       @RequestParam (defaultValue = "1") Integer page,
                       @RequestParam (defaultValue = "10") Integer pageSize){
        log.info("查询参数：name={},label={},page={},pageSize={}", name, label, page, pageSize);
        // 2、调用service
        PageResult<Role> pageResult = roleService.list(name,label,page,pageSize);
        return Result.success(pageResult);
    }
    // 删除
    @DeleteMapping ("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除参数：id={}", id);
        roleService.deleteById(id);
        return Result.success();
    }
    //添加角色
    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("添加角色：{}", role);
        roleService.addRole(role);
        return Result.success();
    }
    // 根据id查询角色
    @GetMapping ("/{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询参数：id={}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    // 修改角色
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("修改参数：{}", role);
        roleService.update(role);
        return Result.success();
    }
    // 查询所有角色
    @GetMapping("/list")
    public Result listAll(){
        log.info("查询所有角色");
        return Result.success(roleService.listAll());
    }
}
